﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Hangjing.SQLServerDAL;
using Hangjing.Common;
using Hangjing.WebCommon;
using Hangjing.Model;
using System.Collections.Generic;
using System.IO;
using MVCNPOIHelper.Common;

/// <summary>
/// 订单列表 
/// </summary>
public partial class shop_selfdispatch : System.Web.UI.Page
{
    private string SqlWhere
    {
        get
        {
            object o = ViewState["SqlWhere"];
            return (o == null) ? string.Empty : o.ToString();
        }
        set
        {
            ViewState["SqlWhere"] = value;
        }
    }

    /// <summary>
    /// 商家编号
    /// </summary>
    private int tid
    {
        get
        {
            object o = ViewState["tid"];
            return (o == null) ? 0 : Convert.ToInt32(o);
        }
        set
        {
            ViewState["tid"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        UserHelp.IsLogin_Togo(Request.Url.PathAndQuery);

        if (!this.Page.IsPostBack)
        {
            InitSqlwhere();
            BindData();

        }
    }

    Deliver dal = new Deliver();
    protected void BindData()
    {

        
        string lat = HjNetHelper.GetQueryString("lat");
        string lng = HjNetHelper.GetQueryString("lng");
        this.rptOrderList.DataSource = dal.shopgetDelivers(lat, lng, SqlWhere);
        this.rptOrderList.DataBind();
        NoRecord();

    }

    private void NoRecord()
    {
        if (rptOrderList.Items.Count == 0)
        {
            noRecord.Style.Add(HtmlTextWriterStyle.Display, "block");
        }
        else
        {
            noRecord.Style.Add(HtmlTextWriterStyle.Display, "none");
        }
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindData();
    }

    protected void rptOrderList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string type = e.CommandName;
        switch (type)
        {
            case "dispatch":

                string[] deliver = e.CommandArgument.ToString().Split('_');
                string orderid = HjNetHelper.GetQueryString("id");

                OrderDeliverInfo odmodel = new OrderDeliverInfo();
                odmodel.Orderid = orderid;
                odmodel.DeliverId = Convert.ToInt32(deliver[0]);
                odmodel.DeliverName = deliver[1];
                odmodel.Dispatcher = "商家pc";
                odmodel.DispatchTime = DateTime.Now;
                odmodel.DeliveryTime = DateTime.Now;
                odmodel.Inve1 = 0;
                odmodel.Inve2 = "";
                odmodel.Section = "3";
                new OrderDeliver().Save(odmodel);


                //记录
                new Custorder().AddOrderRecord(odmodel.Orderid, 7, "商家pc", "商家pc调度给配送员");

                WebUtility.excutesql("UPDATE dbo.Custorder SET ReveVar1 = '0' WHERE orderid = '" + orderid + "'");

                {
                    NoticeHelper notice = new NoticeHelper(Context, odmodel.DeliverId.ToString(), orderid);
                    notice.sendOrderToDeliver();
                }


                AlertScript.RegScript(this.Page, UpdatePanel1, "alert('调度成功，点击继续下单!');gourl('buildorder.aspx')");


                break;
        }
    }


    /// <summary>
    ///  搜索
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void search_Click(object sender, EventArgs e)
    {
        InitSqlwhere();
        BindData();
    }


    /// <summary>
    ///  自动调度 自动派给1公里内距离最近、没单待岗的骑士
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void autodispatch_Click(object sender, EventArgs e)
    {
        string lat = HjNetHelper.GetQueryString("lat");
        string lng = HjNetHelper.GetQueryString("lng");
        string orderid = HjNetHelper.GetQueryString("id");
        PointsInfo shop = UserHelp.GetUser_Togo();
     
        int did = new Custorder().ShopAutoDispatch(lat, lng, orderid, shop.cityid,shop.IsCallCenter);
        if (did == 0)
        {
            WebUtility.excutesql("UPDATE dbo.Custorder SET ReveVar1 = '0' WHERE orderid = '" + orderid + "'");
            AlertScript.RegScript(this.Page, UpdatePanel1, "alert('附近没有待岗骑士，订单直接转由客服调度!');");
        }
        else
        {
            DeliverInfo deliver = new Deliver().GetModel(did);
            AlertScript.RegScript(this.Page, UpdatePanel1, "alert('已经调度给配送员：" + deliver.Name + "(" + deliver.Phone + ")" + "!');gourl('buildorder.aspx');");
        }

    }


    /// <summary>
    ///  订单发给客服调度
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void dispatch2Service_Click(object sender, EventArgs e)
    {
        string lat = HjNetHelper.GetQueryString("lat");
        string lng = HjNetHelper.GetQueryString("lng");
        string orderid = HjNetHelper.GetQueryString("id");
       


        WebUtility.excutesql("UPDATE dbo.Custorder SET ReveVar1 = '0' WHERE orderid = '" + orderid + "'");

        AlertScript.RegScript(this.Page, UpdatePanel1, "alert('订单已经发送给客服');gourl('buildorder.aspx');");


    }

    /// <summary>
    /// 生成查询条件
    /// </summary>
    protected void InitSqlwhere()
    {
        SqlWhere = "  1=1 ";
        PointsInfo shop = UserHelp.GetUser_Togo();
        if (shop.cityid > 0)
        {
            SqlWhere += " and Inve1 = " + shop.cityid;
        }
        if (shop.IsCallCenter > 0)
        {
            SqlWhere += " and GpsIMEI = " + shop.IsCallCenter;
        }


        if (tbdelivername.Text != "")
        {
            SqlWhere += " and name like '%" + WebUtility.InputText(tbdelivername.Text) + "%'";
        }
        if (ddlworkstate.SelectedValue != "-1")
        {
            SqlWhere += " and workstate = " + ddlworkstate.SelectedValue;
        }

        if (tbdeliverphone.Text.Trim() != "")
        {
            SqlWhere += " and Phone like '%" + WebUtility.InputText(tbdeliverphone.Text) + "%'";

        }

        //Response.Write(SqlWhere);


    }

}
